/*
 * CS_REQ_EVENT_DUNGEON_STAMINA_INFO.cpp
 *
 *  Created on: 2013. 9. 2.
 *      Author: mega@gamevil.com
 */

#include "../../User.h"
#include "../../NLogic/Stamina.h"
#include "dungeon.h"

namespace ProjectZ { namespace NCommand { namespace NDungeon {

void CS_REQ_EVENT_DUNGEON_STAMINA_INFO::OnExecute(User * pUser, ::Network::Packet * pPacket)
{
	PREPARE_PACKET;

	NLogic::Stamina * pStamina = pUser->GetStamina();
	if (NULL == pStamina)
	{
		DEBUGLOG("%s : pStamina is NULL!!!", __FUNCTION__);
		SendPacket(pUser, ACK_UNKNOWN_ERROR, 0, 0);
		return;
	}

	int64_t remainSec = pStamina->UpdateKeyRecoveryTime();

	SendPacket(pUser, ACK_OK, pStamina->GetCurrentKey(), remainSec);


/*	time_t currentSec;
	time(&currentSec);

	// 이벤트에 최초로 참여하거나 현재 스태미너가 최대인 경우
	if (pUser->GetCharacterInfo().event_stamina	== -1
			|| pUser->GetCharacterInfo().event_stamina >= EVENT_DUNGEON_STAMINA_MAX)
	{
		DEBUGLOG("%s pUser->GetCharacterInfo().event_stamina : %d", __FUNCTION__, pUser->GetCharacterInfo().event_stamina);
		pUser->GetCharacterInfo().event_stamina = EVENT_DUNGEON_STAMINA_MAX;
		pUser->GetCharacterInfo().recover_stamina_sec = currentSec;
		SendPacket(pUser, ACK_OK, EVENT_DUNGEON_STAMINA_MAX, 0);
		return;
	}

	// 현재 시간 기준 지난 시간 계산
	int elapsedSec = currentSec - pUser->GetCharacterInfo().recover_stamina_sec;

	// 회복 할 스태미너 계산
	int recoveryStamina = elapsedSec / EVENT_DUNGEON_STAMINA_RECOVERY_SEC;

	FILELOG("CS_REQ_EVENT_DUNGEON_STAMINA_INFO : user_seq=%d, slot_seq=%d, elapsed_sec=%d, recover_stamina=%d",
			pUser->GetUserSeq(), pUser->GetCharacterInfo().characterseq, elapsedSec, recoveryStamina);

	int64_t remainSec = 0;

	if (recoveryStamina <= 0)
	{
		// 스태미너 회복 시간이 지나지 않은 경우
		remainSec = EVENT_DUNGEON_STAMINA_RECOVERY_SEC - elapsedSec;
	}
	else
	{
		// 스태미너 회복 시간이 지난 경우
		pUser->GetCharacterInfo().event_stamina += recoveryStamina;

		if (pUser->GetCharacterInfo().event_stamina >= EVENT_DUNGEON_STAMINA_MAX)
		{
			remainSec = 0;
			pUser->GetCharacterInfo().event_stamina = EVENT_DUNGEON_STAMINA_MAX;
			pUser->GetCharacterInfo().recover_stamina_sec = currentSec;
		}
		else
		{
			// 스태미너가 최대가 아니면 남은 시간을 누적한다.
			remainSec = EVENT_DUNGEON_STAMINA_RECOVERY_SEC - (elapsedSec % EVENT_DUNGEON_STAMINA_RECOVERY_SEC);
			pUser->GetCharacterInfo().recover_stamina_sec += (EVENT_DUNGEON_STAMINA_RECOVERY_SEC * recoveryStamina);

		//	int addSec = (EVENT_DUNGEON_STAMINA_RECOVERY_SEC * recoveryStamina) + (modSec - remainSec);
		//	pUser->GetCharacterInfo().recover_stamina_sec += addSec;
		}
	}

	FILELOG("CS_REQ_EVENT_DUNGEON_STAMINA_INFO : user_seq=%d, slot_seq=%d, cur_stamina=%d, remain_sec=%d",
			pUser->GetUserSeq(), pUser->GetCharacterInfo().characterseq, pUser->GetCharacterInfo().event_stamina, remainSec);

	SendPacket(pUser, ACK_OK, pUser->GetCharacterInfo().event_stamina, remainSec);*/
}

void CS_REQ_EVENT_DUNGEON_STAMINA_INFO::SendPacket(User * pUser, int16_t res, uint8_t stamina, uint64_t remainSec)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	*pAck << res << stamina << remainSec;

	pAck->MakePacket(CMD_SC_REQ_EVENT_DUNGEON_STAMINA_INFO);

	pUser->Send(pAck);
}

} /*NDungeon*/ } /*NCommand*/ } /*ProjectZ*/


